﻿/****************
SmartButton
簡單的製作一只 Button, 需於影格上編上 up, over, out, down 等影格名稱(frame label).

20101220 - 自動使用 hitarea 作為 btn 上的觸發範圍.
20101022 - MOUSE_DOWN 改為 Click, 並於 "down" 片段播放完後才 dispatch event.
//**************/
package com.cf.core.model{
	import com.cf.core.model.SmartClipEX;
	import flash.display.Stage;
	import flash.events.*;
	import com.cf.core.events.UIEvent;
	import com.cf.core.Utils.debug;
	public class SmartButton extends SmartClipEX{
		private var __up:String = "up";
		private var __over:String = "over";
		private var __out:String = "out";
		private var __down:String = "down";
		private var __playfix:Boolean = false;
		private var G_value:*;
		public function get value():*{return G_value}
		public function set value(_o:*){G_value=_o}
		
		private var G_btn;
		public function SmartButton():void{
			G_btn=this;
			if( getChildByName("hitarea")!=null){
				G_btn=getChildByName("hitarea");
				mouseChildren = true;
			}else{
				G_btn=this;
				mouseChildren = false;	// Fix : Don't touch my child. touch ME :D
			}
			G_btn.addEventListener(MouseEvent.MOUSE_UP, handleButton, false, 0, true);
			G_btn.addEventListener(MouseEvent.MOUSE_OVER, handleButton, false, 0, true);
			G_btn.addEventListener(MouseEvent.MOUSE_OUT, handleButton, false, 0, true);
			G_btn.addEventListener(MouseEvent.MOUSE_DOWN, handleButton, false, 0, true);
			G_btn.addEventListener(MouseEvent.CLICK, handleButton, false, 0, true);
			buttonMode = true;
			useHandCursor = true;
			
		}//fn
		
		public function set up(_s:String):void{__up=_s}
		public function set over(_s:String):void{__over=_s}
		public function set out(_s:String):void{__out=_s}
		public function set down(_s:String):void{__down=_s}
		private function handleButton(e:MouseEvent):void{
			switch(e.type){
				case MouseEvent.MOUSE_UP:
					// playLoop(__up,false);
					dispatchEvent(new UIEvent(UIEvent.UP, this));
				break;
				case MouseEvent.MOUSE_OVER:
					__playfix = false;
					playOne(__over);
					dispatchEvent(new UIEvent(UIEvent.OVER, this));
				break;
				case MouseEvent.MOUSE_OUT:
					if(! __playfix){
						playOrder([__out,__up]);
					}//if
					__playfix = false;
					dispatchEvent(new UIEvent(UIEvent.OUT, this));
				break;
				case MouseEvent.MOUSE_DOWN:
					dispatchEvent(new UIEvent(UIEvent.DOWN, this));
					// __playfix = true;
				break;
				case MouseEvent.CLICK:
					callback=afterClick;
					playOrder([__down,__up]);
					__playfix = true;
				break;
			}//case
		}//fn
		private function afterClick(){
			dispatchEvent(new UIEvent(UIEvent.CLICK, this));
		}//fn
	}//class
}//package